package com.fhj.movie.fragment;

import java.io.ByteArrayOutputStream;
import java.io.File;

import com.fhj.movie.activity.HyyKeFuActivity;
import com.fhj.movie.activity.HyyLoginActivity;
import com.fhj.movie.activity.HyySettingActivity;
import com.fhj.movie.activity.PicturePopupWindow;
import com.fhj.movie_fhj_01.R;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HyyMineFrgment extends Fragment implements OnClickListener {
	private ImageView setting, zhaoxiang;
	private Button button;
	private TextView keFu;
	private LinearLayout xuanzuo, duihuan, tuangou, youhui, jifen, xiangkan,
			layout;
	PicturePopupWindow menuWindow;
	private static final int NONE = 0;
	private static final int PHOTO_GRAPH = 1;// 拍照
	private static final int PHOTO_ZOOM = 2; // 缩放
	private static final int PHOTO_RESOULT = 3;// 结果
	private static final String IMAGE_UNSPECIFIED = "image/*";

	public HyyMineFrgment() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_mine, container, false);
		setting = (ImageView) view.findViewById(R.id.mine_setting);
		zhaoxiang = (ImageView) view.findViewById(R.id.mine_zhaoxiang);
		button = (Button) view.findViewById(R.id.mine_login);
		keFu = (TextView) view.findViewById(R.id.mine_kefu);
		xuanzuo = (LinearLayout) view.findViewById(R.id.mine_xuanzuo);
		duihuan = (LinearLayout) view.findViewById(R.id.mine_duihuan);
		tuangou = (LinearLayout) view.findViewById(R.id.mine_tuangou);
		youhui = (LinearLayout) view.findViewById(R.id.mine_youhui);
		jifen = (LinearLayout) view.findViewById(R.id.mine_jifen);
		xiangkan = (LinearLayout) view.findViewById(R.id.mine_xiangkan);
		layout = (LinearLayout) view.findViewById(R.id.mine_mine);
		layout.getBackground().setAlpha(0);
		setting.setOnClickListener(this);
		zhaoxiang.setOnClickListener(this);
		button.setOnClickListener(this);
		keFu.setOnClickListener(this);
		xuanzuo.setOnClickListener(this);
		duihuan.setOnClickListener(this);
		tuangou.setOnClickListener(this);
		youhui.setOnClickListener(this);
		jifen.setOnClickListener(this);
		xiangkan.setOnClickListener(this);
		layout.setVisibility(View.INVISIBLE);
		
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.mine_setting:
			Intent intent = new Intent(getActivity(), HyySettingActivity.class);
			startActivity(intent);
			layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.mine_login:
			Intent intent1 = new Intent(getActivity(), HyyLoginActivity.class);
			startActivity(intent1);
			layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.mine_kefu:
			Intent intent2 = new Intent(getActivity(), HyyKeFuActivity.class);
			startActivity(intent2);
			layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.mine_xuanzuo:
			Intent intent3 = new Intent(getActivity(), HyyLoginActivity.class);
			startActivity(intent3);
			layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.mine_duihuan:
			Intent intent4 = new Intent(getActivity(), HyyLoginActivity.class);
			startActivity(intent4);
			layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.mine_tuangou:
			Intent intent5 = new Intent(getActivity(), HyyLoginActivity.class);
			startActivity(intent5);
			layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.mine_youhui:
			Intent intent6 = new Intent(getActivity(), HyyLoginActivity.class);
			startActivity(intent6);
			layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.mine_jifen:
			Intent intent7 = new Intent(getActivity(), HyyLoginActivity.class);
			startActivity(intent7);
			layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.mine_xiangkan:
			Intent intent8 = new Intent(getActivity(), HyyLoginActivity.class);
			startActivity(intent8);
			layout.setVisibility(View.INVISIBLE);
			break;
		case R.id.mine_zhaoxiang:
			menuWindow = new PicturePopupWindow(getActivity(), itemsOnClick);
			// 显示窗口
			menuWindow.showAtLocation(
					getActivity().findViewById(R.id.mine_zhaoxiang),
					Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
			layout.setVisibility(View.VISIBLE);
			layout.getBackground().setAlpha(125);
			break;
		case R.id.mine_mine:
			layout.getBackground().setAlpha(0);
			break;
		default:
			break;
		}
	}

	private OnClickListener itemsOnClick = new OnClickListener() {

		public void onClick(View v) {
			menuWindow.dismiss();
			switch (v.getId()) {
			case R.id.mine_cancle:
				menuWindow.dismiss();
				layout.setVisibility(View.INVISIBLE);
				break;
			case R.id.mine_bendi:
				Intent intent = new Intent(Intent.ACTION_PICK, null);
				intent.setDataAndType(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
						IMAGE_UNSPECIFIED);
				startActivityForResult(intent, PHOTO_ZOOM);
				layout.setVisibility(View.INVISIBLE);
				break;
			case R.id.mine_paizhao:
				Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri
						.fromFile(new File(Environment
								.getExternalStorageDirectory(), "temp.jpg")));
				startActivityForResult(intent2, PHOTO_GRAPH);
				layout.setVisibility(View.INVISIBLE);
				break;
			default:
				break;
			}

		}

	};

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == NONE)
			return;
		// 拍照
		if (requestCode == PHOTO_GRAPH) {
			// 设置文件保存路径
			File picture = new File(Environment.getExternalStorageDirectory()
					+ "/temp.jpg");
			startPhotoZoom(Uri.fromFile(picture));
		}

		if (data == null)
			return;

		// 读取相册缩放图片
		if (requestCode == PHOTO_ZOOM) {
			startPhotoZoom(data.getData());
		}
		// 处理结果
		if (requestCode == PHOTO_RESOULT) {
			Bundle extras = data.getExtras();
			if (extras != null) {
				Bitmap photo = extras.getParcelable("data");
				ByteArrayOutputStream stream = new ByteArrayOutputStream();
				photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0-100)压缩文件
				// 此处可以把Bitmap保存到sd卡中，具体请看：http://www.cnblogs.com/linjiqin/archive/2011/12/28/2304940.html
				zhaoxiang.setImageBitmap(photo); // 把图片显示在ImageView控件上
			}

		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 收缩图片
	 * 
	 * @param uri
	 */
	public void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");// 调用Android系统自带的一个图片剪裁页面,
		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
		intent.putExtra("crop", "true");// 进行修剪
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX", 300);
		intent.putExtra("outputY", 500);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, PHOTO_RESOULT);
	}

}